var searchData = {size:3, start: 0};
var reload = false; // 记录是重新加载数据，还是分页追加
$(document).ready(function() {
	$(".search-btm").click(function(){
		reload = true;
		searchOrder();
	});
	$("#loadMore").click(function(){
		reload = false;
		searchOrder();
	});
	$(".order-view-tap ul li").click(activyTab);
	loadOrders();
});

/**
 * 切换tab页
 */
function activyTab() {
	// 清空搜索框
	$(".search-input").val("");
	searchData.mobile = "";
	searchData.code = "";
	// 取消所有选中
	$(".order-view-tap ul li").removeClass("activy");
	$(this).addClass("activy");
	// 设置订单状态
	if($(this).attr("code")) {
		searchData.status = $(this).attr("code");
	}
	reload = true;
	loadOrders();
}

/**
 * 根据点击判断是隐藏还是显示订单商品
 */
function showWares() {
	var waresUl = $("#wares_" + $(this).attr("oid"));
	var opStr = "";
	if(waresUl.css("visibility") == "visible") {
		opStr = "查看";
		waresUl.css("visibility","hidden");
		waresUl.hide();
	} else {
		opStr = "隐藏";
		waresUl.css("visibility","visible");
		waresUl.show();
	}
	$(this).find("li").html(opStr + "订单商品详情");
}

/**
 * 搜索订单号
 */
function searchOrder() {
	searchData.mobile = $("#mobileSearch").val();
	searchData.code = $("#codeSearch").val();
	loadOrders();
}
/**
 * 点击搜索和分页加载数据
 */
function loadOrders() {
	if(reload) {
		searchData.size = 3;
		searchData.start = 0;
	}
	$.ajax({type:"POST", url: "/order/orderContent.html",
		data: searchData,
		dataType: "html",
		success: function(html) {
			if(html && html.trim().length > 10) {
				searchData.start += searchData.size;
				if(reload) {
					$(".order-list").remove();
				}
				$("#loadMore").before(html);
				// 重新绑定修改事件
				$(".btn-border-white,ul[name='showWares']").unbind("click");
				$(".btn-border-white").click(modifyOrderStatus);
				// 隐藏或显示订单商品
				$("ul[name='showWares']").click(showWares);
			} else {
				$("#loadMoreText").html("没有更多数据啦！");
				// 去掉绑定事件 TODO GXG
				
			}
		}, error : function() {
			showOpResultLayer("加载数据出错");
		}
	});
}

// 以下为订单操作代码，后续考虑独立一个js文件，为列表页和详情页调用
/**
 * 修改订单状态弹出框
 */
function modifyOrderStatus() {
	var data = {};
	data.id = $(this).attr("orderId");
	data.op = $(this).attr("op");
	data.status = $(this).attr("nextStatus");
	var html = [];
	html.push('<div style="text-align: left;">');
	html.push('<label>备注：</label><br><textarea id="remark" class="order-modify-textarea" value=""></textarea>');
	html.push('</div>');
	
	layer.open({
		content: html.join("")
		,shadeClose: false
    	,btn: ['取消', '修改']
    	,yes: function(index){
      		layer.close(index);
    	}
    	,no: function(index) {
    		$.ajax({type:"POST", url: "/order/status/"+data.id+".html",
				data: data,
				dataType: "json",
				success: function(d) {
					if(d && d.code == 1) {
						showModify(data);
						layer.close(index);
					} else {
						showOpResultLayer("修改订单状态失败！");
					}
				}, error : function() {
					showOpResultLayer("修改订单状态出错！");
				}
			});
    	}
  	});
}

/**
 * 回显修改后的值，避免重新加载
 */
function showModify(data) {
	// 修改按钮 
	var btnHtml = [];
	var status;
	switch (data.status) {
		case "SENT":
			status = "已发货";
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="COMPLETED" op="COMPLETE" >完成</div>');
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUND_GOODS" op="REFUND_GOODS" >退货</div>');
			break;
		case "REFUND_APPLY":
			status = "申请退款";
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUNDING" op="AUDIT_REFUND" >同意</div>');
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUND_FAIL" op="AUDIT_REFUND" >拒绝</div>');
			break;
		case "REFUNDING":
			status = "退款中";
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUNDED" op="RIMIT" >打款</div>');
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUND_FAIL" op="COMPLETE" >拒绝</div>');
			break;
		case "REFUND_GOODS":
			status = "申请退货";
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUNDING_GOODS" op="AUDIT_REFUND_GOODS" >同意</div>');
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUND_GOODS_FAIL" op="AUDIT_REFUND_GOODS" >拒绝</div>');
			break;
		case "REFUNDING_GOODS":
			status = "退货中";
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUND_APPLY" op="REFUND" >退款</div>');
			btnHtml.push('<div class="btn-border-white" orderId='+data.id+' nextStatus="REFUND_GOODS_FAIL" op="COMPLETE" >拒绝</div>');
			break;
		case "COMPLETED":
			status = "已完成";
		case "REFUND_FAIL":
			status = "退款失败";
		case "REFUNDED":
			status = "已退款";
		case "REFUND_GOODS_FAIL":
			status = "退货失败";
			break;
		default:
			break;
	}
	$("#op_" + data.id).html(btnHtml.join(""));
	// 修改状态
 	if(status) {
 		$("#status_" + data.id).html(status);
 	}

	$(".btn-border-white").unbind("click");
	$(".btn-border-white").click(modifyOrderStatus);
}
/**
 * 弹出框，显示操作结果
 * @param content 结果内容
 */
function showOpResultLayer(content) {
	layer.open({
		content: content
    	,btn: ['关闭']
    	,yes: function(index){
      		layer.close(index);
    	}
	});
}